<?php

class ServicesController extends AppController {

    var $name = "Services";

    function index($poll_id) {
        $this->set('usuario', $this->Auth->user());
        $this->Services->recursive = 0;
        $this->set('services', $this->Service->find('all', array('conditions' => array('Service.poll_id' => $poll_id))));
        $this->data['Service']['poll_id'] = $poll_id;

        //verifico si esta encuesta ya fue bloqueada, si es asi no se podran
        //imprimir los links para adicionar o modificar servicios.
        App::Import('model', 'Poll');
        $p = new Poll();
        $p->recursive = -1;

        $this->set('visto_bueno', $p->find('first', array('conditions' => array('Poll.id' => $poll_id), 'fields' => array('Poll.visto_bueno'))));
    }

    function add($poll_id) {
        if (!empty($this->data)) {
            if ($this->Service->saveAll($this->data)) {
                $this->Session->setFlash("Información de servicios adicionada");
                $this->redirect(array('controller' => "Services", 'action' => 'index', $poll_id));
            }
        } else {
            $this->set('poll_id', $poll_id);
            $this->data['Service']['poll_id'] = $poll_id;
        }
    }

    function edit($id, $poll_id) {
        if (!empty($this->data)) {
            if ($this->Service->saveAll($this->data)) {
                $this->Session->setFlash("Información de servicios adicionada");
                $this->redirect(array('controller' => "Services", 'action' => 'index', $poll_id));
            }
        } else {
            $this->data = $this->Service->find("first", array("conditions" => array("Service.id" => $id)));
        }
    }

    function delete($id, $poll_id) {
        if ($this->Service->delete($id)) {
            $this->Session->setFlash('Información de servicios borrada con exito');
            $this->redirect(array('controller' => 'Services', 'action' => 'index', $poll_id));
        }
    }

}

?>